package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.dto.userEmp.EmpResumeParamsDto;
import com.zlm.common.dto.userEmp.EmpResumeResponseDto;
import com.zlm.common.entity.*;
import com.zlm.common.enums.SalaryRangeEnum;
import com.zlm.common.enums.WorkTypeEnum;
import com.zlm.common.util.DateTimeUtils;
import com.zlm.user.emp.dao.*;
import com.zlm.user.emp.service.EmpResumeService;
import com.zlm.user.emp.util.EmpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: duyufu
 * @Date: 2019/5/15 15:54
 */
@Service
public class EmpResumeServiceImpl extends AbstractBaseService<EmpResumeDao, EmpResumeDO> implements EmpResumeService {

    @Autowired
    private SysTimeSectionDao sysTimeSectionDao;
    @Autowired
    private BasPostDao basPostDao;

    @Autowired
    @Override
    public void setBaseDao(EmpResumeDao empResumeDao) {
        this.baseDao = empResumeDao;
    }

    @Override
    public EmpResumeDO queryResumeById(Integer empId) {
        EmpResumeDO empResumeDO = baseDao.queryResumeById(empId);
        if (null != empResumeDO) {
            if (null != empResumeDO.getResumeStyle()) {
                empResumeDO.setResumeStyleName(WorkTypeEnum.getNameByCode(empResumeDO.getResumeStyle()));
            }
            empResumeDO.setSalaryName(SalaryRangeEnum.getNameByCode(empResumeDO.getSalary()));
        }
        return empResumeDO;
    }

    @Override
    public Integer insertEmpResume(EmpResumeParamsDto empResumeParamsDto) {
        return baseDao.insertEmpResume(empResumeParamsDto);
    }

    @Override
    public Integer countEmpResume(Integer empId) {
        return baseDao.countEmpResume(empId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveEmpResume(EmpResumeParamsDto empResumeParamsDto) {
        //保存用户期望信息
        Integer resumeId = null;
        BasPostDO basPostDO = basPostDao.queryPostById(empResumeParamsDto.getPostId());
        empResumeParamsDto.setPostName(basPostDO.getPostName());
        Integer count = baseDao.countEmpResume(empResumeParamsDto.getEmpId());
        if (count == 0) {
            //新增简历
            baseDao.insertEmpResume(empResumeParamsDto);
            resumeId = empResumeParamsDto.getResumeId();
        }else if (count == 1) {
            //修改
            EmpResumeDO empResumeDO = baseDao.queryResumeById(empResumeParamsDto.getEmpId());
            resumeId = empResumeDO.getResumeId();
            empResumeParamsDto.setResumeId(resumeId);
            baseDao.updateEmpResume(empResumeParamsDto);
        }else {
            throw new RuntimeException();
        }
        //保存期望工作时间
        List<SysTimeSectionDO> sysTimeSectionList = new ArrayList<>();
        List<String> sysTimeSections = new ArrayList<>();
        if (null != empResumeParamsDto.getSysTimeSections()){
            sysTimeSections = empResumeParamsDto.getSysTimeSections();
        }

        for (String sysTimeSection : sysTimeSections) {
            SysTimeSectionDO sysTimeSectionDO = new SysTimeSectionDO();
            String[] split = sysTimeSection.split("~");
            sysTimeSectionDO.setStartDate(DateTimeUtils.string1Date(split[0]));
            sysTimeSectionDO.setEndDate(DateTimeUtils.string1Date(split[1]));
            sysTimeSectionDO.setResumeId(resumeId);
            sysTimeSectionDO.setIdentitys(1);
            sysTimeSectionList.add(sysTimeSectionDO);
        }
        //保存期望培训时间
        List<String> trainTimeSections = new ArrayList<>();
        if (null != empResumeParamsDto.getTrainTimeSections()) {
            trainTimeSections = empResumeParamsDto.getTrainTimeSections();
        }
        for (String trainTimeSection : trainTimeSections) {
            SysTimeSectionDO sysTimeSectionDO = new SysTimeSectionDO();
            String[] split = trainTimeSection.split("~");
            sysTimeSectionDO.setStartDate(DateTimeUtils.string1Date(split[0]));
            sysTimeSectionDO.setEndDate(DateTimeUtils.string1Date(split[1]));
            sysTimeSectionDO.setResumeId(resumeId);
            sysTimeSectionDO.setIdentitys(2);
            sysTimeSectionList.add(sysTimeSectionDO);
        }
        //删除之前保存的期望工作时间
        sysTimeSectionDao.deleteSysTimeSection(resumeId);
        if (sysTimeSectionList.size() > 0) {
            //保存时间
            sysTimeSectionDao.insertTimeSectionList(sysTimeSectionList);
        }
        EmpUtils.updateEmpPerfect(empResumeParamsDto.getEmpId());
    }

    @Override
    public EmpResumeResponseDto getResume(Integer empId) {
        return EmpUtils.getResume(empId);
    }

}


